﻿using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;

using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.ExtensibleStorage;

using System.Xml;



namespace YJKMepElecWeak
{
    /// <summary>
    /// 导线赋值
    /// </summary>
    public partial class WireAttrbutionDlg : System.Windows.Forms.Form
    {
        public int ifpipelink = 1;
        public int relativeh = 1;
        //       public List <ConduitType> conduitTypeV = new List <ConduitType>;
        public ElementId[] conduitTypeIdV = new ElementId[200];
        public string[] conduitDiaV = new string[20];
        public int numconduitTypeV = 0;

        public static List<WireType> wireTypeV = new List<WireType>();
        public static List<ElementId> wireTypeIdV = new List<ElementId>();
        public string[] wireAreaV = new string[200];
        public int numwireTypeV = 0;

        public Wire bWire;

        public static Document doc;
        public static string filespathE = "";
        public static string filename = "YJKElectricSetting.xml";
        public   WireAssignParameters aapara = new WireAssignParameters();
        public string[] layout = new string[20];
        public int layoutnum = 10;

        public List<WireTypeSelfDataDef> listWire = new List<WireTypeSelfDataDef>();
        public List<ConduitTypeSelfDataDef> listConduit = new List<ConduitTypeSelfDataDef>();
        public static int wireTypeIndexselected = 0;

        public static string wireTypeselected = "";

        public static int wireSizeIndexSelected = 0;
        public static double wireSizeSelected = 0;


        public static int conduitTIndexSelected = 0;
        public static string conduitTSelected = "";
        public static int conduitSizeIndexSelected = 0;
        public static int  conduitSizeSelected = 0;
         public static int numSelected=1;
         public static double heightSelected = 2.7;
         public static string layoutModeSelected = "";
        public static int selectedindexd=0;
        public static ElementId wireTypeSelectedId ;
        public ExternalCommandData m_Revit;

        public WireAttrbutionDlg(ExternalCommandData cmdData)
        {
            this.m_Revit = cmdData;
            InitializeComponent();
        }
        
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
          [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
        public void initDefaultValue(WireAssignParameters a)
        {
            wireTypeList.Text = a.WireTypeName;
            for (int i = 0; i < listWire.Count; i++)
            {
                string wirename = listWire[i].wireTypeName;
                wireTypeList.Items.Add(wirename);
            }

            GetWireSizeofWire(a.selectedWireType);
            wireTypeList.SelectedIndex = a.selectedWireType;
            wireNum.Value = a.wirenum;
            wireArea.Text = a.wiresize.ToString();
            layoutMode.Text = a.layoutmanner;

            conduitType.Text = a.conduitT;
            for (int i = 0; i < listConduit.Count; i++)
            {
                string wirename = listConduit[i].conduitTypeName;
                conduitType.Items.Add(wirename);
            }
            GetConduitSizeofWire(a.selectedconduitType);
            conduitType.SelectedIndex = a.selectedconduitType;
            conduitDia.Text = a.conduitsize.ToString();
            conduitHeight.Text = a.conduitheight.ToString();
            wireTypeSelectedId = a.selectedWireTypeId;
            

        }

        public void GetControlParameter(ref WireAssignParameters aapara1)
        {
         //   aapara1 = aapara;
            



            aapara1.WireTypeName=wireTypeselected;
            aapara1.selectedWireType=wireTypeIndexselected;
            aapara1.wirenum=numSelected;
            aapara1.wiresize=wireSizeSelected;
            aapara1.wiresizeSelected=wireSizeIndexSelected;
            aapara1.layoutmanner=layoutModeSelected;
          //  aapara1.layoutmanneri;
          //  aapara1.loopnumt=1;
            aapara1.conduitT=conduitTSelected;
            aapara1. selectedconduitType=conduitTIndexSelected;
            aapara1.conduitsize=conduitSizeSelected;

            aapara1.conduitsizeSelected=conduitSizeIndexSelected;
            aapara1.conduitheight=heightSelected ;
            aapara1.selectedWireTypeId=wireTypeSelectedId;
           }


        public void initDefault()
        {
            layoutnum = 10;
            layout[0] = "AB";
            layout[1] = "BC";
            layout[2] = "AC";
            layout[3] = "CLC";
            layout[4] = "WS";
            layout[5] = "WC";
            layout[6] = "CE";
            layout[7] = "CC";
            layout[8] = "SCE";
            layout[9] = "FC";
            for (int i = 0; i < layoutnum; i++)
                layoutMode.Items.Add(layout[i]);

           



        }


        public void GetStoreVariable(out WireAssignParameters a)
        {

            a = new WireAssignParameters();
            a.WireTypeName = wireTypeList.Text;
            a.selectedWireType = wireTypeIndexselected;
            a.wirenum = (int)wireNum.Value;
            a.wiresize = Convert.ToDouble(wireArea.Text);
            a.layoutmanner = layoutMode.Text;
            //        a.loopnumt= loopnum.Value;
            a.conduitT = conduitType.Text;
            a.selectedconduitType = conduitTIndexSelected;
            a.conduitsize = Convert.ToInt32(conduitDia.Text);
            a.conduitsizeSelected=conduitSizeSelected;
            a.conduitheight = Convert.ToDouble(conduitHeight.Text);


        }


        public void SetControlParameter(int indexv)
        {
            
              selectedindexd = indexv;
            // Convert.ToDouble(textBox1.Text) / 1000
        }


        private void wireTypeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selected = wireTypeList.SelectedIndex;
            if (wireTypeIndexselected != selected)
            {
       
                wireTypeSelectedId=listWire[selected].wireTypeId;
                aapara.selectedWireType = selected;
                wireTypeIndexselected = selected;
                wireTypeselected = listWire[selected].wireTypeName;
                aapara.WireTypeName = listWire[selected].wireTypeName;
                YJKCommandElec.AssignWireAttribution.mark = 0;
                SendMessageESC();

                GetWireSizeofWire(selected);
                Update();
            }
         
              
        }
        public void SendMessageESC()
        {
         /*   if (selectedindexd != 0)
            {
              YJKCommandElec.AssignWireAttribution.mark = 0;
              YJKMepAcsPipe.UI.CommanDlgFun.PostEsc();
            } */
        }
        public void GetWireSizeofWire(int selected)
        {
          //  wireArea.ResetText();
              aapara.wiresizeSelected = 0;
             wireArea.Items.Clear();
            int i = 0;
            foreach (KeyValuePair<double, double> kvp in listWire[selected].WireSizes)
            {
                wireArea.Items.Add(kvp.Key.ToString());
               if (i == 0)
                {
                    aapara.wiresize = kvp.Key;
                 
                    wireSizeIndexSelected = 0;
                    wireSizeSelected = kvp.Key;



                } 
               i = i + 1;
            }
           wireTypeIndexselected = selected;
            wireArea.SelectedIndex = 0;
           wireTypeselected = listWire[wireTypeIndexselected].wireTypeName;

    //        conduitSizeIndexSelected = 0;

            wireArea.Update();
        }


        public void GetConduitSizeofWire(int selected)
        {
            aapara.conduitsizeSelected = 0;
            conduitDia.Items.Clear();
            int i = 0;
            for (int j = 0; j < listConduit[selected].conduitSizes.Count; j++)
            {
                int kvp = listConduit[selected].conduitSizes[j];
                conduitDia.Items.Add(kvp);
                if (i == 0)
                {
                    aapara.conduitsize = kvp;
                    conduitSizeIndexSelected = 0;
                    conduitSizeSelected = kvp;
                }
                i = i + 1;
            }

            conduitSizeIndexSelected = 0;
            conduitTSelected = listConduit[selected].conduitTypeName;
            conduitDia.SelectedIndex = aapara.conduitsizeSelected;

        }
      
        private void wireArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            aapara.wiresize = wireArea.SelectedIndex;
            wireSizeIndexSelected = wireArea.SelectedIndex;

        //    KeyValuePair<double, double> kvp = new KeyValuePair<double, double>();
            int i=0;
            foreach (KeyValuePair<double, double> kvp in listWire[wireTypeIndexselected].WireSizes)
            {
                if (i == wireSizeIndexSelected)
                {
                    wireSizeSelected = kvp.Key;
                    break;
                }
                i=i+1;

            }
      //      wireTypeIndexselected = wireArea.SelectedIndex;
            wireTypeselected = listWire[wireTypeIndexselected].wireTypeName;

            SendMessageESC();

        }

        private void conduitType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selected = conduitType.SelectedIndex;
            if (selected != conduitSizeSelected)
            {

            //    wireTypeIndexselected = wireArea.SelectedIndex;
            //    wireTypeselected = listWire[wireTypeIndexselected].wireTypeName;
              //  conduitType.Items.Clear();
                 conduitTIndexSelected = selected;
                 conduitTSelected=listConduit[conduitTIndexSelected].conduitTypeName;
                 GetConduitSizeofWire(selected);
                 SendMessageESC();

            }
        }

        private void conduitDia_SelectedIndexChanged(object sender, EventArgs e)
        {
            aapara.conduitsizeSelected=conduitDia.SelectedIndex;
            conduitSizeIndexSelected=conduitDia.SelectedIndex;

            SendMessageESC();
          //  aapara.conduitsize =
        }

        private void layoutMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            int layoutModeIndexSelected=layoutMode.SelectedIndex;
            layoutModeSelected = (string)layoutMode.Items[layoutModeIndexSelected];
            SendMessageESC();
        }

        private void wireNum_ValueChanged(object sender, EventArgs e)
        {
            numSelected =(int) wireNum.Value;
            SendMessageESC();
        }

        private void conduitHeight_TextChanged(object sender, EventArgs e)
        {
            heightSelected = Convert.ToDouble(conduitHeight.Text.ToString());
            SendMessageESC();
        }

        private void WireAttrbutionDlg_MouseLeave(object sender, EventArgs e)
        {
               if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
                  {
                     int wMsg = 7;
                     WireAttrbutionDlg.SendMessage(YJKCommandElec.AssignWireAttribution._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
                     return;
                   }
                   base.Focus();
        }

        private void WireAttrbutionDlg_FormClosing(object sender, FormClosingEventArgs e)
        {
             int bVk = 27;
             int dwFlags = 0;
             int dwFlags2 = 2;
             WireAttrbutionDlg.keybd_event((byte)bVk, 0, dwFlags, 0);
             WireAttrbutionDlg.keybd_event((byte)bVk, 0, dwFlags2, 0);
        }

     
    }
    public class WireTypeSelfDataDef
    {

        public WireTypeSelfDataDef(string wireTypename, ElementId id1)
        {
            wireTypeName = wireTypename;
            wireTypeId = id1;
        }
        public string wireTypeName { get; set; }
        public Dictionary<double, double> WireSizes = new Dictionary<double, double>();
        public ElementId wireTypeId { get; set; }
    }

    public class ConduitTypeSelfDataDef
    {

        public ConduitTypeSelfDataDef(string conduittypename, ElementId id1)
        {
            conduitTypeName = conduittypename;
            conduitTypeId = id1;

        }
        public string conduitTypeName { get; set; }
        public List<int> conduitSizes = new List<int>();
        public ElementId conduitTypeId { get; set; }
    }
    public class WireAssignParameters
    {

        public string WireTypeName;
        public int selectedWireType;

        public ElementId selectedWireTypeId;
        public int wirenum;
        public double wiresize;
        public int wiresizeSelected;
        public string layoutmanner;
        public int layoutmanneri;
        public string loopnumt;
        public string conduitT;
        public int selectedconduitType;
        public int conduitsize;

        public int conduitsizeSelected;
        public double conduitheight;


    }

}

